<template>
  <div class="order-detail">
    <div class="container">
      <el-breadcrumb separator="/" class="breadcrumb">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/user' }">个人中心</el-breadcrumb-item>
        <el-breadcrumb-item>订单详情</el-breadcrumb-item>
      </el-breadcrumb>

      <el-card v-if="order" class="order-info-card">
        <template #header>
          <span>订单详情</span>
        </template>
        <el-descriptions :column="2" border>
          <el-descriptions-item label="订单号">{{ order.id }}</el-descriptions-item>
          <el-descriptions-item label="下单时间">{{ formatDate(order.createTime) }}</el-descriptions-item>
          <el-descriptions-item label="收货人">{{ order.receiverName }}</el-descriptions-item>
          <el-descriptions-item label="联系电话">{{ order.receiverPhone }}</el-descriptions-item>
          <el-descriptions-item label="收货地址" :span="2">{{ order.receiverAddress }}</el-descriptions-item>
          <el-descriptions-item label="订单总价" :span="2">
            <span class="total-price">¥{{ order.totalPrice }}</span>
          </el-descriptions-item>
          <el-descriptions-item label="订单状态" :span="2">
            <el-tag :type="statusTagType(order.status)">{{ statusText(order.status) }}</el-tag>
          </el-descriptions-item>
        </el-descriptions>
        <div class="order-items">
          <h3>商品明细</h3>
          <el-table :data="order.items" border stripe>
            <el-table-column prop="productName" label="商品名称" />
            <el-table-column prop="productPrice" label="单价" width="100">
              <template #default="scope">
                ¥{{ scope.row.productPrice }}
              </template>
            </el-table-column>
            <el-table-column prop="quantity" label="数量" width="80" />
            <el-table-column prop="subtotal" label="小计" width="100">
              <template #default="scope">
                ¥{{ scope.row.subtotal.toFixed(2) }}
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
      <el-empty v-else description="订单不存在或已被删除" />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { getOrderDetail } from '@/api/order'

const route = useRoute()
const order = ref(null)
const orderId = route.params.orderId

const fetchOrder = async () => {
  if (!orderId) return
  const response = await getOrderDetail(orderId)
  if (response.data) {
    order.value = response.data
  }
}

const formatDate = (dateString) => {
  if (!dateString) return '未知'
  return new Date(dateString).toLocaleString('zh-CN')
}

const statusText = (status) => {
  switch (status) {
    case 0: return '待支付'
    case 1: return '已支付'
    case 2: return '已发货'
    case 3: return '已完成'
    case 4: return '已取消'
    default: return '未知'
  }
}
const statusTagType = (status) => {
  switch (status) {
    case 0: return 'info'
    case 1: return 'success'
    case 2: return 'warning'
    case 3: return 'success'
    case 4: return 'danger'
    default: return 'info'
  }
}

onMounted(() => {
  fetchOrder()
})
</script>

<style scoped>
.order-detail {
  padding: 20px 0;
}
.container {
  max-width: 900px;
  margin: 0 auto;
  padding: 0 20px;
}
.breadcrumb {
  margin-bottom: 30px;
}
.order-info-card {
  margin-bottom: 30px;
}
.total-price {
  color: #e74c3c;
  font-size: 20px;
  font-weight: bold;
}
.order-items {
  margin-top: 20px;
}
</style> 